package com.ktjy.ktmall.dao.impl;

import com.ktjy.ktmall.dao.AddressDao;
import com.ktjy.ktmall.dao.BaseDao;
import com.ktjy.ktmall.entity.AddressParams;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @version v1.0
 * @Author 郴州科泰
 * @Create 2024-07-30 - 10:12
 */
public class AddressDaoImpl extends BaseDao implements AddressDao {

    // 新增收货地址
    @Override
    public int insert(AddressParams address) {
        String sql = "insert into t_address(`user_id`,`user_name`,`tel_number`,fullLocation,`province`,`city`,`district`,`detail`,`is_default`) values(?,?,?,?,?,?,?,?,?)";
        return super.executeUpdate(sql, address.getUserId(), address.getUserName(),address.getTelNumber(),address.getFullLocation(),address.getProvince(),address.getCity(),address.getDistrict(), address.getDetail(),address.getIsDefault());
    }

    /**
     * 通过用户id查询此用户的收货地址列表
     * @param userId 用户id
     */
    @Override
    public List<AddressParams> findByUserId(Integer userId) {
        List<AddressParams> list = new ArrayList<>();
        String sql = "select * from t_address where `user_id` = ?";
        rs = super.executeQuery(sql, userId);
        try {
            while (rs.next()) {
                AddressParams a = new AddressParams(rs.getInt("id"),rs.getInt("user_id"),rs.getString("user_name"),rs.getString("tel_number"),rs.getString("fullLocation"),rs.getString("province"),rs.getString("city"),rs.getString("district"),rs.getString("detail"),rs.getInt("is_default"));
                list.add(a);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;
    }

    /**
     * 通过地址id查询地址的详情信息
     * @param id 地址id
     */
    @Override
    public AddressParams findById(int id) {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Connection conn = this.getConnection();
        AddressParams a = null;
        String sql = "select * from t_address where `id` = ?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setObject(1, id);
            rs = pstmt.executeQuery();

            while (!rs.isClosed() && rs.next()) {
                a = new AddressParams(rs.getInt("id"),rs.getInt("user_id"),rs.getString("user_name"),rs.getString("tel_number"),rs.getString("fullLocation"),rs.getString("province"),rs.getString("city"),rs.getString("district"),rs.getString("detail"),rs.getInt("is_default"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return a;
    }

    /**
     * 修改收货地址信息
     * @param address 要修改的收货地址对象
     */
    @Override
    public int update(AddressParams address) {
        String sql = "update t_address set `user_name` = ?,`tel_number` = ?,fullLocation = ?,`province` = ?,`city` = ?,`district` = ?,`detail` = ?,`is_default` = ? where id = ?";
        return super.executeUpdate(sql, address.getUserName(),address.getTelNumber(),address.getFullLocation(),address.getProvince(),address.getCity(),address.getDistrict(), address.getDetail(),address.getIsDefault(), address.getId());
    }

    /**
     * 通过地址id删除指定的收货地址
     * @param id 地址id
     */
    @Override
    public int delById(int id) {
        String sql = "delete from t_address where id = ?";
        return super.executeUpdate(sql, id);
    }

    /**
     * 根据登录用户id查询其默认地址信息。
     * @param userId 登录用户的id
     */
    @Override
    public AddressParams getDefaultAddress(int userId) {
        AddressParams a = null;
        String sql = "select * from t_address where `user_id` = ? AND is_default = 1";
        rs = super.executeQuery(sql, userId);
        try {
            if (rs.next()) {
                a = new AddressParams(rs.getInt("id"),rs.getInt("user_id"),rs.getString("user_name"),rs.getString("tel_number"),rs.getString("fullLocation"),rs.getString("province"),rs.getString("city"),rs.getString("district"),rs.getString("detail"),rs.getInt("is_default"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return a;
    }
}
